<template>
  <div
    class="login-container"
    :style="{ backgroundImage: 'url(' + initialLoginBg + ')' }"
  >
    <login-canvas />
    <div class="login-box">
      <div class="box__left">
        <img :src="bloodImage" class="blood-image" alt="">
      </div>
      <div class="box__right">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
          @submit.native.prevent
        >
        <div class="box__right--title-pre">
            {{ preTitle }}
          </div>
          <div class="box__right--title-pre">
            {{ preTitle }}
          </div>
          <div class="box__right--title">
            {{ initialTitle }}
          </div>
          <div class="box__right--slogan">
            欢迎来到智慧喷灌系统，请登录！
          </div>
          <el-form-item prop="phone" class="box__right--item">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="phone"
              v-model.trim="loginForm.phone"
              placeholder="请输入手机号"
              name="phone"
              type="text"
              tabindex="1"
              auto-complete="off"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password" class="box__right--item">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              v-if="!openPassword"
              ref="password"
              v-model="passwordSymbol"
              placeholder="请输入密码"
              tabindex="2"
              @input="inputDataConversion"
            />
            <el-input
              v-else
              ref="password"
              v-model="loginForm.password"
              placeholder="请输入密码"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="!openPassword ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
          <el-form-item v-if="false" prop="captcha" class="box__right--item">
            <span class="svg-container">
              <svg-icon icon-class="form" />
            </span>
            <el-input
              ref="captcha"
              v-model.trim="loginForm.captcha"
              placeholder="请输入验证码"
              name="captcha"
              type="text"
              tabindex="3"
              auto-complete="off"
              clearable
              style="width: 200px"
            />
            <el-image
              style="float: right; width: 140px; height: 47px"
              class="code"
              :src="codeUrl"
              @click="changeCode"
            />
          </el-form-item>
          <el-button
            size="default"
            :loading="loading"
            type="primary"
            style="width: 100%; margin-top: 8px"
            native-type="submit"
            @click="handleLogin"
          >立即登录</el-button>
        </el-form>
      </div>
    </div>
    <div v-if="webconfig.icpShow" class="copyright">
      © {{ year }} {{ company }} {{ webconfig.companyName }} 版权所有
      {{ webconfig.icp }}
    </div>
  </div>
</template>

<script>
import LoginCanvas from './component/LoginCanvas'
import { webconfig } from '@/utils/constant'
import { getWebconfig } from '@/api/initial'
import { getDomain } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  name: 'Login',
  components: {
    LoginCanvas
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('密码不能少于1位'))
      } else {
        callback()
      }
    }
    return {
      preTitle: '国家重点研发计划项目（2023YFD1900700）',
      codeUrl: 'http://api.zesi.com.cn/api/index/captcha',
      openPassword: false,
      passwordSymbol: '',
      company: '',
      webconfig,
      bloodImage: '/assets/data.png',
      loginForm: {
        phone: '',
        password: '',
        captcha: ''
      },
      loginRules: {
        phone: [
          { required: true, trigger: 'blur', message: '请输入正确的手机号' }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
        // captcha: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      year: new Date().getFullYear()
    }
  },
  computed: {
    ...mapGetters(['initialTitle', 'initialLoginBg'])
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getWebconfig()
  },
  methods: {
    changeCode() {
      this.codeUrl = `http://api.zesi.com.cn/api/index/captcha?timestamp=${new Date().getTime()}`
    },
    async getWebconfig() {
      const domain = getDomain() || ''
      const { data } = await getWebconfig({ domain })
      if (data.title) {
        this.company = data.company
        this.$store.commit('initial/SET_INITIAL', data)
      }
    },
    handleFocus() {
      this.$refs.password = 'password'
    },
    handleBlur() {
      this.$refs.password = 'text'
    },
    showPwd() {
      this.openPassword = !this.openPassword
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.changeCode()
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    inputDataConversion(value) {
      const newValue = value.replace(/●/g, '')
      newValue ? (this.loginForm.password += newValue) : null
      let data = ''
      for (let i = 0; i < value.length; i++) {
        data += '●'
      }
      this.passwordSymbol = data
    },
    checkOpenPassword() {
      this.openPassword = !this.openPassword
      console.log(`this.openPassword `, this.openPassword)
      if (!this.openPassword) {
        let data = ''
        for (let i = 0; i < this.loginForm.password.length; i++) {
          data += '●'
        }
        this.passwordSymbol = data
      }
    }
  }
}
</script>

<style lang="scss">
.blood-image {
  width: 240px;
  margin: 20px auto;
}
.login-container {
  & .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    & .el-input__inner {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;
      -webkit-appearance: none;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #fafafa;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  position: relative;
  & .login-box {
    display: flex;
    width: 1080px;
    height: 560px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    overflow: hidden;
    // box-shadow: 4px 4px 10px 4px #f1f1f1;
    z-index: 9;
    & .box__left {
      width: 40%;
      text-align: center;
      background: url("~@/icons/login/bg.png") no-repeat center bottom 36px;
      background-color: rgba(16, 162, 226, 0.9);
      // background: url('~@/icons/login/bg.png') no-repeat center bottom 36px, radial-gradient(ellipse ,#10a2e2, #338cd2);
      padding: 49px;
      box-sizing: border-box;
      & .box__left--title {
        font-size: 26px;
        color: #ffffff;
        text-align: center;
        margin-top: 43px;
      }
      & .box__left--content {
        width: 80%;
        margin: 22px auto 22px;
        font-size: 14px;
        color: #ffffff;
        text-align: center;
        line-height: 18px;
        letter-spacing: 4px;
      }
    }
    & .box__right {
      width: 60%;
      padding: 80px 56px;
      box-sizing: border-box;
      background-color: rgba(255, 255, 255, 0.9);
       & .box__right--title-pre {
        font-size: 24px;
        color: #303030;
        text-align: center;
        margin: 0 0 12px 0;
      }
      & .box__right--title {
        font-size: 20px;
        color: #303030;
        text-align: center;
        margin: 12px 0 24px 0;
      }
      & .box__right--slogan {
        font-size: 14px;
        color: #606060;
        text-align: center;
        margin-bottom: 80px;
      }
      & .box__right--item {
        background-color: #ffffff;
        padding-left: 14px;
        border: 1px solid #d0d0d0;
        box-sizing: border-box;
      }
    }
  }
  & .copyright {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 45px;
    font-size: 14px;
    color: #f0f0f0;
  }
}
</style>
